import {reqgetcoupon} from '../../request/api'
import {createSelector} from 'reselect'
const initState={
    list:[],
    n:0
}

const types={
    CHANGE_LIST:'YOUHUI_CHANGELIST',
    CHNAGE_N:'YOUHUI_CHANGEN'
}

const reducer =(state=initState,action)=>{
        switch(action.type){
            case types.CHANGE_LIST:
                return {
                    ...state,
                    list:action.arr
                }
            case types.CHNAGE_N:
                 return {
                    ...state,
                    n:action.n
                }
            default:
                return state
        }
}   

export const actions={
    changelist:(arr)=>({type:types.CHANGE_LIST,arr}),

    changen:n=>({type:types.CHNAGE_N,n}),
    reqlist:()=>(dispatch, getState)=>{
        reqgetcoupon().then(res=>{
            dispatch(actions.changelist(res.data.list))
        })
    }
}

//导出数据
export const getlist=state=>state.youhui.list
export const getn = state => state.youhui.n
export const getshowlist=createSelector(
    [getlist,getn],
    (list,n) => {
       return list[n]
    }
)
export default reducer